package cn.net.dckj.framework.web.service;

import cn.net.dckj.common.config.ServerConfig;
import cn.net.dckj.common.utils.file.ftp.FTPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;

/**
 * RuoYi首创 html调用 thymeleaf 实现参数管理
 *
 * @author dckj
 */
@Service("util")
public class UtilService {
    @Autowired
    private ServerConfig serverConfig;

    public String calculateFileSize(Long fileSize) {
        if (fileSize != null) {
            DecimalFormat df = new DecimalFormat("#.00");
            if (fileSize < 1024) {
                return fileSize + " B";
            }
            if (fileSize < 1024 * 1024) {
                return df.format((double) fileSize / 1024) + " KB";
            }
            if (fileSize < 1024 * 1024 * 1024) {
                return df.format((double) fileSize / (1024 * 1024)) + " MB";
            }
            return df.format((double) fileSize / (1024 * 1024 * 1024)) + " GB";
        }
        return "0 B";
    }

    public String getPreviewUrl(String ftpPathName) {
        return serverConfig.getUrl() + FTPUtils.getInstance().getPreviewUrl(ftpPathName);
    }
}
